#include <bits/stdc++.h>
// 2025/01/21
// tag: 
// Author: Zhang Muen
using namespace std;

class UF
{
public:
    vector<int> parents;
    int cnt;

    UF(int n)
    {
        cnt = n;
        for (int i = 0; i < n; i++)
            parents.push_back(i);
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};

int n, m, p, q;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> p >> q;
    UF uf(n + m + 1);
    uf.un(1, n + 1);
    for (int i = 1; i <= p; i++)
    {
        int x, y;
        cin >> x >> y;
        uf.un(x, y);
    }
    for (int i = 1; i <= q; i++){
        int x, y;
        cin >> x >> y;
        uf.un(-x + n, -y + n);
    }
    int t = uf.find(1), man = 0, woman = 0;
    for (int i = 1; i <= n; i++){
        if (uf.find(i) == t)
            man++;
    }
    for (int i = 1; i <= m; i++){
        if (uf.find(i + n) == t)
            woman++;
    }
    cout << min(man, woman);
    return 0;
}